<?php
namespace app\common\model;

use think\facade\Db;

class Interests extends BaseModel
{
    public function __construct(array $data = [])
    {
        parent::__construct($data);

    }

    /**
     * 列表
     * @param condition 条件 array
     * @param pageSize 每页展示条数 int
     * @param page 页数 int
     * @param order 排序 string
     * @return array
     */
    public static function list($condition, $pageSize = 15, $page = 1, $order)
    {
        $list =  self::getList($field = '*', $condition, $order, $pageSize, $page);

        return $list;
    }

    /**
     * 修改
     * @param params 参数 array
     * @return bool
     */
    public static function editName($params)
    {
        return self::where(['id' => $params['id']])->update(['name' => $params['sort'], 'update_time' => time()]);
    }

    /**
     * 修改状态
     * @param params 参数 array
     * @return bool
     */
    public static function status($params)
    {
        $info = self::getOne($params['id']);
        $status = 'status';
        $info->$status = $info->$status == 1 ? 0 : 1;
        Db::startTrans();
        try {
            $info->save();
            Db::name('class_type')->where(['id' => $info['id']])->update(['status' => $info->$status]);
            Db::commit();//提交事务
            return true;
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return false;
        }
    }

    /**
     * 删除
     * @param ids id array
     * @return bool
     */
    public static function deleteD($ids)
    {
        $info = self::where([['id', 'in', $ids]])->select();
        Db::startTrans();
        try {
            foreach ($info as $key => $val) {
                $info->delete();
                Db::name('class_type')->where(['id' => $val['id']])->delete();
            }

            Db::commit(); //提交事务
            return true;
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return false;
        }
    }
}